home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / libx11 / libx11 / x11text.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  14KB  |  497 lines

  1. /* Copyright (c) 1996 by Terje Pedersen.  All Rights Reserved   */
  2. /*                                                              */
  3. /* By using this code you will agree to these terms:            */
  4. /*                                                              */
  5. /* 1. You may not use this code for profit in any way or form   */
  6. /*    unless an agreement with the author has been reached.     */
  7. /*                                                              */
  8. /* 2. The author is not responsible for any damages caused by   */
  9. /*    the use of this code.                                     */
  10. /*                                                              */
  11. /* 3. All modifications are to be released to the public.       */
  12. /*                                                              */
  13. /* Thats it! Have fun!                                          */
  14. /* TP                                                           */
  15. /*                                                              */
  16.  
  17. /***
  18.    NAME
  19.      text
  20.    PURPOSE
  21.      text drawing functions
  22.    NOTES
  23.      
  24.    HISTORY
  25.      Terje Pedersen - Mar 21, 1995: Created.
  26.  
  27. 7. Nov 96: Added comment headers to all functions and cleaned the code up
  28.            somewhat. If you have the manual pages you may notice an eerie
  29.        similarity..
  30. ***/
  31.  
  32. #include "amiga.h"
  33. #include "libX11.h"
  34.  
  35. /*#define XLIB_ILLEGAL_ACCESS 1*/
  36.  
  37. #include <X11/Xutil.h>
  38. #include <X11/Intrinsic.h>
  39. #include <X11/IntrinsicP.h>
  40. #include <X11/CoreP.h>
  41.  
  42. #include <X11/Xlibint.h>
  43.  
  44. #include "x11display.h"
  45. #include "x11text.h"
  46.  
  47. /********************************************************************************/
  48. /* external */
  49. /********************************************************************************/
  50.  
  51. extern int X11muiapp;
  52. extern int X11FunctionMapping[];
  53.  
  54. /********************************************************************************/
  55. /* internal */
  56. /********************************************************************************/
  57.  
  58. #ifdef DEBUGXEMUL_ENTRY
  59. int bIgnoreText = 1; /* ignore outputting information about text */
  60. int bSkipText = 0;
  61. #endif
  62.  
  63. /********************************************************************************
  64. Function : XDrawString()
  65. Author   : Terje Pedersen
  66. Input    : 
  67.      display   Specifies  a  connection  to  an  X  server;  returned  from
  68.                XOpenDisplay().
  69.  
  70.      drawable  Specifies the drawable.
  71.  
  72.      gc        Specifies the graphics context.
  73.  
  74.      x
  75.      y
  76.                Specify the x and y coordinates  of  the  baseline  starting
  77.                position  for  the  character, relative to the origin of the
  78.                specified drawable.
  79.  
  80.      string    Specifies the character string.
  81.  
  82.      length    Specifies the number of characters in string.
  83.  
  84. Output   : 
  85. Function : draw an 8-bit text string, foreground only.
  86. ********************************************************************************/
  87.  
  88. boolean
  89. X11SimpleTextClip( char* string, char* Xtempstr, int length, int* x, int* y )
  90. {
  91.   int nWidth;
  92.   int nNewWidth;
  93.  
  94.   nWidth = TextLength(DG.drp,Xtempstr,length);
  95.   if( *x+nWidth>DG.vWinWidth ){ /* shave off some characters at the end of the string */
  96.     int vNewLength = length;
  97.  
  98.     if( length==1 )
  99.       return FALSE;
  100.     nNewWidth = DG.vWinWidth-*x;
  101.     vNewLength = (int)(length*nNewWidth/nWidth);
  102.     while( TextLength(DG.drp,Xtempstr,vNewLength)>nNewWidth && vNewLength>0 ) {
  103.       vNewLength--;
  104.     }
  105.     if( !vNewLength )
  106.       return FALSE;
  107.     
  108.     length = vNewLength;
  109.     Xtempstr[length] = 0;
  110.   }
  111.   if( *y-DG.drp->Font->tf_Baseline<0 ){ 
  112.     return FALSE; /* The simple solution.. */
  113.   }
  114.   if( *y>DG.vWinHeight ){ 
  115.     return FALSE; /* The simple solution.. */
  116.   }
  117.   if( *x<0 ){ /* shave off some characters in the front of the string */
  118.     int vNewLength;
  119.     int vAdd;
  120.     
  121.     vNewLength = (int)(length*-*x/nWidth);
  122.     if( !vNewLength ){
  123.       vNewLength = 1;
  124.     }
  125.     while( (vAdd = TextLength(DG.drp,Xtempstr,vNewLength))<-*x && vNewLength>0 ) {
  126.       vNewLength++;
  127.     }
  128.     if( vNewLength>length )
  129.       return FALSE;
  130.     length -= vNewLength;
  131.     *x += vAdd;
  132.     strncpy(Xtempstr,&string[vNewLength],length);
  133.     Xtempstr[length] = 0;
  134.   }
  135.  
  136.   return TRUE;
  137. }
  138.  
  139. XDrawString( Display* d,
  140.          Drawable win,
  141.          GC gc,
  142.          int x,
  143.          int y,
  144.          char* string,
  145.          int length )
  146.   char Xtempstr[256];
  147.   struct IntuiText itext;
  148.  
  149.   strncpy(Xtempstr,string,length);
  150.   Xtempstr[length] = 0;
  151.  
  152. #ifdef DEBUGXEMUL_ENTRY
  153.   if( !bIgnoreText )
  154.  
  155.   if( bSkipText )
  156.     return;
  157. #endif
  158.   if( win!=DG.vPrevWindow )
  159.     if( !(DG.drp=setup_win(win)) )
  160.       return;
  161. #if 1
  162.   if( gc!=vPrevGC )
  163.     setup_gc(gc);
  164. #endif
  165.  
  166.   if( X11Drawables[win]==X11WINDOW ){
  167.     x -= X11Windows[X11DrawablesMap[win]].RelX;
  168.     y -= X11Windows[X11DrawablesMap[win]].RelY;
  169. #if 0
  170. /* def OPTDBG */
  171.     printf("(drawing)XDrawString [%s] to %d (%d %d) -> (%d %d) rel %d %d\n",Xtempstr,win,x,y,DG.vWinX+x,DG.vWinY+y,X11Windows[X11DrawablesMap[win]].RelX,X11Windows[X11DrawablesMap[win]].RelY); 
  172. #endif
  173.   }
  174.  
  175.   if( x>DG.vWinWidth || length<1 || !string )
  176.     return;
  177.  
  178.   if( gc->values.font /*&&!X11muiapp*/ ){
  179.     ULONG new;
  180.  
  181.     struct TextFont *tf = (struct TextFont*)((sFont*)gc->values.font)->tfont;
  182.     struct TextAttr *tattr = (struct TextAttr*)((sFont*)gc->values.font)->tattr;
  183.  
  184.     SetFont(DG.drp,tf);
  185.     new = SetSoftStyle(DG.drp,tattr->ta_Style ^ tf->tf_Style,(FSF_BOLD|FSF_UNDERLINED|FSF_ITALIC));
  186.  
  187. /*    if(tf->tf_Flags==42||tf==(struct TextFont*)DG.X11GC->values.font)StripFont(tf);*/
  188.   }
  189.  
  190. #ifndef DOCLIPPING
  191.   /* if I am not using intuition clipping I have to clip the text myself */
  192.   if( !X11SimpleTextClip(string,Xtempstr,length,&x,&y) )
  193.     return;
  194. #endif
  195.  
  196.   itext.IText = (char *)Xtempstr;
  197.   itext.LeftEdge = 0;
  198.   itext.TopEdge = 0;
  199. #if 0
  200.   if( gc->values.function==GXinvert || gc->values.function==GXxor ){
  201.     itext.DrawMode = COMPLEMENT;
  202.   } else /* if( gc->values.background==X11DrawablesBackground[win] ){*/
  203.     itext.DrawMode = JAM1;
  204. #else
  205.   itext.DrawMode = X11FunctionMapping[gc->values.function];
  206. #endif
  207.   itext.ITextFont = NULL;
  208.   itext.NextText = NULL;
  209.   itext.FrontPen = gc->values.foreground;
  210.   itext.BackPen = gc->values.background;
  211.  
  212. #if 0
  213.   if( gc->values.foreground == X11DrawablesBackground[win] )
  214.     printf("Invisible?\n");
  215.  
  216.   itext.DrawMode = JAM2;
  217. #endif
  218.  
  219.   PrintIText(DG.drp,&itext,DG.vWinX+x,DG.vWinY+y-DG.drp->Font->tf_Baseline);
  220. }
  221.  
  222. /********************************************************************************
  223. Function : XDrawImageString()
  224. Author   : Terje Pedersen
  225. Input    : 
  226.      display   Specifies  a  connection  to  an  X  server;  returned  from
  227.                XOpenDisplay().
  228.  
  229.      drawable  Specifies the drawable.
  230.  
  231.      gc        Specifies the graphics context.
  232.  
  233.      x
  234.      y
  235.                Specify the x and y coordinates  of  the  baseline  starting
  236.                position  for  the  image  text  character,  relative to the
  237.                origin of the specified drawable.
  238.  
  239.      string    Specifies the character string.
  240.  
  241.      length    Specifies the number of characters in the string argument.
  242.  
  243. Output   : 
  244. Function : draw 8-bit image text characters.
  245. ********************************************************************************/
  246.  
  247. XDrawImageString( Display* display,
  248.           Window win,
  249.           struct _XGC* gc,
  250.           int x,
  251.           int y,
  252.           char* string,
  253.           int length )
  254. {
  255.   char Xtempstr[256];
  256.   struct IntuiText itext;
  257.   int origx,origy;
  258.  
  259. #ifdef DEBUGXEMUL_ENTRY
  260.   if( !bIgnoreText )
  261.     printf("(events)XDrawImageString %d,%d %s (%d)in window %d\n",x,y,string,length,(int)win);
  262.   if( bSkipText )
  263.     return;
  264. #endif 
  265.   if( win!=DG.vPrevWindow )
  266.     if( !(DG.drp=setup_win(win)) )
  267.       return;
  268.   if( gc!=vPrevGC )
  269.     setup_gc(gc);
  270.  
  271.   if( gc->values.font /*&&!X11muiapp*/ ){
  272.     struct TextFont *tf = (struct TextFont*)((sFont*)gc->values.font)->tfont;
  273. /*
  274.     struct TextAttr *tattr = (struct TextAttr*)((sFont*)gc->values.font)->tattr;
  275. */
  276.     SetFont(DG.drp,tf);
  277. /*    if(tf->tf_Flags==42||tf==(struct TextFont*)DG.X11GC->values.font)StripFont(tf);*/
  278.   }
  279.   origx = DG.vWinX+x;
  280.   origy = DG.vWinY+y-DG.drp->Font->tf_Baseline;
  281.  
  282.   if( length<1 || !string ) {
  283.     /*printf("zero length string in xdrawimagestring\n");*/ 
  284.     return;
  285.   }
  286.  
  287.   strncpy(Xtempstr,string,length);
  288.   Xtempstr[length] = 0;
  289.  
  290. #ifndef DOCLIPPING
  291.   /* if I am not using intuition clipping I have to clip the text myself */
  292.   if( !X11SimpleTextClip(string,Xtempstr,length,&x,&y) )
  293.     return;
  294. #endif
  295.  
  296.   itext.IText = Xtempstr;
  297.   itext.LeftEdge = 0;
  298.   itext.TopEdge = 0;
  299. #if 1
  300.   if( gc->values.function==GXinvert || gc->values.function==GXxor ){
  301.     itext.DrawMode = COMPLEMENT;
  302.   } else {
  303.     itext.DrawMode = JAM2;
  304.   }
  305. #else
  306.   itext.DrawMode = X11FunctionMapping[gc->values.function];
  307. #endif
  308.   itext.ITextFont = NULL;
  309.   itext.NextText = NULL;
  310.   itext.FrontPen = gc->values.foreground;
  311.   itext.BackPen = gc->values.background;
  312.   PrintIText(DG.drp,&itext,origx,origy);
  313. }
  314.  
  315. /********************************************************************************
  316. Function : XDrawText16()
  317. Author   : Terje Pedersen
  318. Input    : 
  319.      display   Specifies  a  connection  to  an  X  server;  returned  from
  320.                XOpenDisplay().
  321.  
  322.      drawable  Specifies the drawable.
  323.  
  324.      gc        Specifies the graphics context.
  325.  
  326.      x
  327.      y
  328.                Specify the x and y coordinates  of  the  baseline  starting
  329.                position  for  the initial string, relative to the origin of
  330.                the specified drawable.
  331.  
  332.      items     Specifies a pointer to an array of text items using two-byte
  333.                characters.
  334.  
  335.      nitems    Specifies the number of text items in the array.
  336.  
  337. Output   : 
  338. Function : draw 16-bit polytext strings.
  339. ********************************************************************************/
  340.  
  341. XDrawText16( Display* display,
  342.          Drawable drawable,
  343.          GC gc,
  344.          int x,
  345.          int y,
  346.          XTextItem16* items,
  347.          int nitems )
  348. {
  349. #if (DEBUGXEMUL_ENTRY) || (DEBUGXEMUL_WARNING)
  350.   printf("WARNING: XDrawText16\n");
  351. #endif
  352.  
  353.   return(0);
  354. }
  355.  
  356. /********************************************************************************
  357. Function : XDrawString16()
  358. Author   : Terje Pedersen
  359. Input    : 
  360.      display   Specifies  a  connection  to  an  X  server;  returned  from
  361.                XOpenDisplay().
  362.  
  363.      drawable  Specifies the drawable.
  364.  
  365.      gc        Specifies the graphics context.
  366.  
  367.      x
  368.      y
  369.                Specify the x and y coordinates  of  the  baseline  starting
  370.                position  for  the  character, relative to the origin of the
  371.                specified drawable.
  372.  
  373.      string    Specifies the character string.  Characters  are  two  bytes
  374.                wide.
  375.  
  376.      length    Specifies the number of characters in string.
  377.  
  378. Output   : 
  379. Function : draw two-byte text strings.
  380. ********************************************************************************/
  381.  
  382. XDrawString16( Display* display,
  383.            Drawable drawable,
  384.            GC gc,
  385.            int x,
  386.            int y,
  387.            char* string,
  388.            int length )
  389. {
  390. #if (DEBUGXEMUL_ENTRY)
  391.   if( !bIgnoreText )
  392.     printf("XDrawString16\n");
  393. #endif
  394.   XDrawString(display,drawable,gc,x,y,string,length);
  395.  
  396.   return(0);
  397. }
  398.  
  399. /********************************************************************************
  400. Function : XDrawImageString16()
  401. Author   : Terje Pedersen
  402. Input    : 
  403.      display   Specifies a connection to an X server; returned from
  404.                XOpenDisplay().
  405.  
  406.      drawable  Specifies the drawable.
  407.  
  408.      gc        Specifies the graphics context.
  409.  
  410.      x
  411.      y
  412.                Specify the x and y coordinates of the baseline starting
  413.                position for the image text character, relative to the
  414.                origin of the specified drawable.
  415.  
  416.      string    Specifies the character string.
  417.  
  418.      length    Specifies the number of characters in the string argument.
  419.  
  420. Output   : 
  421. Function : draw 16-bit image text characters.
  422. ********************************************************************************/
  423.  
  424. XDrawImageString16( Display* display,
  425.             Drawable win,
  426.             GC gc,
  427.             int x,
  428.             int y,
  429.             char* string,
  430.             int length )
  431. {
  432. #ifdef DEBUGXEMUL_ENTRY
  433.   if( !bIgnoreText )
  434.     printf("XDrawImageString16\n");
  435. #endif
  436.   XDrawImageString(display,win,gc,x,y,string,length);
  437.  
  438.   return(0);
  439. }
  440.  
  441.  
  442. /********************************************************************************
  443. Function : XDrawText()
  444. Author   : Terje Pedersen
  445. Input    : 
  446.      display   Specifies  a  connection  to  an  X  server;  returned  from
  447.                XOpenDisplay().
  448.  
  449.      drawable  Specifies the drawable.
  450.  
  451.      gc        Specifies the graphics context.
  452.  
  453.      x
  454.      y
  455.                Specify the x and y coordinates  of  the  baseline  starting
  456.                position  for  the initial string, relative to the origin of
  457.                the specified drawable.
  458.  
  459.      items     Specifies a pointer to an array of text items.
  460.  
  461.      nitems    Specifies the number of text items in the items array.
  462.  
  463. Output   : 
  464. Function : draw 8-bit polytext strings.
  465. ********************************************************************************/
  466.  
  467. XDrawText( Display* display,
  468.        Drawable drawable,
  469.        GC gc,
  470.        int x,
  471.        int y,
  472.        XTextItem* items,
  473.        int nitems )
  474. {
  475. #if (DEBUGXEMUL_ENTRY) || (DEBUGXEMUL_WARNING)
  476.   printf("WARNING: XDrawText\n");
  477. #endif
  478.  
  479.   return(0);
  480. }
  481.  
  482. XTextExtents16(font_struct, string, nchars, direction_return,
  483.                font_ascent_return, font_descent_return, overall_return)
  484.      XFontStruct *font_struct;
  485.      XChar2b *string;
  486.      int nchars;
  487.      int *direction_return;
  488.      int *font_ascent_return, *font_descent_return;
  489.      XCharStruct *overall_return;
  490. {/*          File 'auxtext.o' */
  491. #if (DEBUGXEMUL_ENTRY) || (DEBUGXEMUL_WARNING)
  492.   printf("WARNING: XTextExtents16\n");
  493. #endif
  494.   return(0);
  495. }
  496.